

--drop trigger [Check_ZLSJ_JDQB]   

alter trigger [dbo].[Check_ZLSJ_JDQB]                                                                        
 on [dbo].[SK_JDQB_BILL]                                                                        
 for update  

 as   

 Declare @FEntryID int
 DECLARE @F_YR_BILLNO VARCHAR(50)
 Declare @F_SK_BILLNO varchar(50)
 Declare @F_SK_DATE varchar(50)
 Declare @F_SK_AMOUNT decimal(19,2)
 declare @F_SK_FYX varchar(50)
 declare @FCheckerID int
 declare @FID int
 declare @FALLAmount decimal(19,2)
 declare @i int

 if update(F_PAEZ_CHECKER)    
begin  
   select @FCheckerID=isnull(F_PAEZ_CHECKER,0), @FID= FID from inserted   

    if @FCheckerID>0 
	   begin
	       
		   Delete from SK_JDQB_BILLENTRYS where FID =@FID

		   set @i =1;

		   declare auth_cur cursor for

	       select distinct F_YR_BILLNO from SK_JDQB_BILLENTRY

		   open auth_cur

		   fetch next from auth_cur into @F_YR_BILLNO

		   while (@@fetch_status=0)

			begin
			 
			 SELECT @FALLAmount=sum(F_YR_AMOUNT) FROM SK_JDQB_BILLENTRY WHERE F_YR_BILLNO =@F_YR_BILLNO

			 IF @FALLAmount > 0
			   BEGIN

				  insert into Z_KD_JDQB_BILLENTRYS (column1) values (1)
				  select @FEntryID = id from Z_KD_JDQB_BILLENTRYS
				  delete from Z_KD_JDQB_BILLENTRYS

				  select TOP 1 @F_SK_FYX=F_YR_FYX,@F_SK_DATE=CONVERT(VARCHAR(10),F_YR_DATETIME,126) from SK_JDQB_BILLENTRY where F_YR_BILLNO =@F_YR_BILLNO and F_YR_AMOUNT>0
			
			      INSERT INTO SK_JDQB_BILLENTRYS(FID,FEntryID,F_SK_BILLNO,F_SK_DATE,F_SK_AMOUNT,F_SK_FYX,FSeq,F_SK_REBILLNO,F_SK_NOTE)
				   SELECT @FID,@FEntryID,@F_YR_BILLNO,@F_SK_DATE,@FALLAmount,@F_SK_FYX,@i,'',''

				  set @i =@i+1
			   END


			fetch next from auth_cur into @F_YR_BILLNO

			END

			close auth_cur

			deallocate auth_cur

	   end
end


